package com.dianrui.moonfire.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.dianrui.moonfire.R;
import com.dianrui.moonfire.bean.Agree;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AgreeListAdapter extends BaseAdapter {

    private Context context;
    private List<Agree>agreeList;
    AgreementClickListener agreementClickListener;

    public AgreeListAdapter(Context context, List<Agree> agreeList) {
        this.context = context;
        this.agreeList = agreeList;
    }

    public void refresh(List<Agree> list) {
        this.agreeList = list;
        this.notifyDataSetChanged();
    }
    public interface AgreementClickListener {
        void onAgreeClick(String contractId);

    }

    public void setOnAgreeClickListener(AgreementClickListener agreementClickListener) {
        this.agreementClickListener = agreementClickListener;
    }

    @Override
    public int getCount() {
        if (agreeList.size() == 0) {
            return 0;
        }
        return agreeList.size();
    }

    @Override
    public Object getItem(int position) {
        return agreeList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.agree_list_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != agreeList && agreeList.size()>0) {
            final Agree agree = agreeList.get(position);
            holder.orderCode.setText(agree.order_code);
            holder.createTime.setText("订购时间:"+agree.create_time);
            holder.number.setText("订购数量:"+agree.number);
            holder.money.setText("订单金额:"+agree.money);
            switch (agree.status){
                case "0":
                    holder.status.setText(context.getResources().getString(R.string.unpay));
                    holder.status.setTextColor(ContextCompat.getColor(context, R.color.nav_color_txt));
                    break;
                case "1":
                    holder.status.setText(context.getResources().getString(R.string.going));
                    holder.status.setTextColor(ContextCompat.getColor(context, R.color.red));
                    break;
                case "2":
                    holder.status.setText(context.getResources().getString(R.string.finished));
                    holder.status.setTextColor(ContextCompat.getColor(context, R.color.green));
                    break;

                case "3":
                    holder.status.setText(context.getResources().getString(R.string.canceled));
                    holder.status.setTextColor(ContextCompat.getColor(context, R.color.nav_color_txt));
                    break;

                default:
                        break;
            }
            holder.showContracts.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (agreementClickListener != null) {
                        agreementClickListener.onAgreeClick(agree.contract_id);
                    }
                }
            });

        }

        return convertView;
    }



    class ViewHolder {
        @BindView(R.id.order_code)
        TextView orderCode;
        @BindView(R.id.status)
        TextView status;
        @BindView(R.id.money)
        TextView money;
        @BindView(R.id.create_time)
        TextView createTime;
        @BindView(R.id.number)
        TextView number;
        @BindView(R.id.show_contract)
        Button showContracts;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
